package com.zhinong.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhinong.dto.ExpertPageQueryDTO;
import com.zhinong.entity.Expert;
import com.zhinong.entity.Product;
import com.zhinong.mapper.ExpertMapper;
import com.zhinong.result.PageResult;
import com.zhinong.service.ExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExpertServiceImpl implements ExpertService {


    @Autowired
    private ExpertMapper expertMapper;
    @Override
    public PageResult pageQuery(ExpertPageQueryDTO expertPageQueryDTO) {
        if(expertPageQueryDTO.getPage() == 0 && expertPageQueryDTO.getPageSize() == 0){
            expertPageQueryDTO.setPage(1);
            expertPageQueryDTO.setPageSize(10);
        }
        PageHelper.startPage(expertPageQueryDTO.getPage(),expertPageQueryDTO.getPageSize());
        Page<Expert> page = expertMapper.pageQuery(expertPageQueryDTO);
        long total = page.getTotal();
        List<Expert> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public Expert getByID(Long id) {
        Expert expert = expertMapper.getById(id);
        return expert;
    }

    @Override
    public void save(Expert expert) {
        expertMapper.save(expert);
    }

    @Override
    public void update(Expert expert) {
        expertMapper.update(expert);
    }

    @Override
    public void delete(Long id) {
        expertMapper.deleteById(id);
    }
}
